package com.yangyouli.unity_demo_server.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.log4j.Logger;

public class PropertiesHelper {
	public static final String PROPERTIES_PATH = "configPath.properties";
	
	static Logger log = Logger.getLogger(PropertiesHelper.class);
	
	public static String getPropertiesValue (String path, String key) {
		Properties properties = new Properties();
	    // 使用ClassLoader加载properties配置文件生成对应的输入流
	    InputStream in = PropertiesHelper.class.getClassLoader().getResourceAsStream(path);
	    // 使用properties对象加载输入流
	    try {
			properties.load(in);
		} catch (IOException e) {
			log.error("获取指定路径的配置 properties 文件的 value 值发生异常", e);
		}
	    //获取key对应的value值
	    return properties.getProperty(key);
	}
	
	/**
	 * 返回配置文件中所有的键值对
	 * @param path
	 * @return
	 */
	public static Map<String, String> getPropertiesValueMap (String path) {
		Properties properties = new Properties();
	    // 使用ClassLoader加载properties配置文件生成对应的输入流
	    InputStream in = PropertiesHelper.class.getClassLoader().getResourceAsStream(path);
	    // 使用properties对象加载输入流
	    try {
			properties.load(in);
		} catch (IOException e) {
			log.error("获取指定路径的配置 properties 文件的 value 值发生异常", e);
		}
	    
	    Set<Object> e = properties.keySet();
	    Map<String, String> map = new HashMap<>();
	    for (Object k : e) {
	    	map.put((String)k, properties.getProperty((String)k));
	    }
	    return map;
	}
	
	/**
	 * 获取指定路径的配置 configPath.properties 文件的 value 值
	 * @param key
	 * @return
	 */
	public static String getPropertiesValue (String key) {
		return getPropertiesValue(PROPERTIES_PATH, key);
	}
}
